<template>
	<view>
		<view class="app-background"></view>
		<my-navbar title="我的发布" :border="true"></my-navbar>
		<view v-if="noticeList.code" class="myPublish">
			<template v-if="noticeList.list.length">
				<NoticeList :list="noticeList.list" :count="noticeList.count" @handle="noticeList.handle"></NoticeList>
			</template>
			<template v-else>
				<view class="myPublish-none">
					<image class="none-image" src="/static/my/image_none.png"></image>
					<view class="none-text">您还没有发布过的通知</view>
				</view>
			</template>
		</view>
		<safe-area></safe-area>
	</view>
</template>

<script>
import { mapState } from 'vuex';
import NoticeList from '@/components/notice-list/notice-list';
import util from '@/common/util';
import sign from '@/common/sign.js'
export default {
	data() {
		return {
			noticeList: {
				pageNo: 1,
				pageSize: 10,
				pageCount: 0,
				count: 0,
				code: 0,
				list: [],
				getList: ifRefresh => {
					return new Promise(async (reslove, reject) => {
						const context = this.noticeList;
						if (ifRefresh) {
							context.pageNo = 1;
							context.pageCount = 0;
							context.count = 0;
						}
						if (context.pageCount !== 0 && context.pageNo >= context.pageCount + 1) {
							this.$showToast('已加载全部通知列表数据');
							reject();
							return;
						}
						const page = {
							pageSize: context.pageSize,
							pageNo: context.pageNo
						};
						const data = {};
						uni.showLoading({
							mask: true
						});
						let res1;
						try {
							res1 = await this.$api.user.get_notice_list({ page, data });
						} catch (err) {
							context.code = err.code;
							uni.hideLoading();
							this.$showToast(err.message || '获取通知列表数据失败');
							reject();
							return;
						}
						context.code = res1.code;
						context.pageCount = res1.result.pageCount;
						context.count = res1.result.count;
						const platform = uni.getSystemInfoSync().platform;
						if (platform === 'ios') {
							res1.result.data.sort((a, b) => {
								const timeA =  new Date(a.cretime.replace(/-/g, '/')).getTime();
								const timeB = new Date(b.cretime.replace(/-/g, '/')).getTime();
								return timeB - timeA;
							});
							for (const i of res1.result.data) {
								i.friendlyTime = util.getFriendlyTime1(new Date(i.cretime.replace(/-/g, '/')));
							}
						} else {
							res1.result.data.sort((a, b) => {
								const timeA =  new Date(a.cretime).getTime();
								const timeB = new Date(b.cretime).getTime();
								return timeB - timeA;
							});
							for (const i of res1.result.data) {
								i.friendlyTime = util.getFriendlyTime1(new Date(i.cretime));
							}
						}
						if (ifRefresh) context.list = [];
						context.list.push(...res1.result.data);
						uni.hideLoading();
						reslove();
					});
				},
				handle: (fn, e, payload) => {
					if (fn === 'clickCard') this.noticeList.handleClickCard(payload.item);
				},
				handleClickCard: item => {
					uni.navigateTo({
						url: '/packageA/pages/notice/noticeDetail?id=' + item.id
					});
				}
			}
		};
	},
	async onLoad() {
		try {
			await this.noticeList.getList(true);
		} catch (err) {}
	},
	async onShow(){
		if(sign.UPDATE_NOTICE_LIST) {
			sign.UPDATE_NOTICE_LIST = false;
			uni.pageScrollTo({
				scrollTop: 0
			});
			try {
				await this.noticeList.getList(true);
			} catch (err) {}
		}
	},
	async onPullDownRefresh() {
		try {
			await this.noticeList.getList(true);
		} catch (err) {}
		uni.stopPullDownRefresh();
	},
	async onReachBottom() {
		this.noticeList.pageNo++;
		try {
			await this.noticeList.getList();
		} catch (err) {}
	},
	components: {
		NoticeList
	}
};
</script>

<style lang="less">
.myPublish {
	padding: 0 30rpx;
}

.myPublish-none {
	display: flex;
	flex-direction: column;
	align-items: center;

	position: absolute;
	left: 50%;
	top: 40%;
	transform: translate(-50%, -50%);
	.none-image {
		width: 334rpx;
		height: 255rpx;
		margin-top: 100rpx;
	}
	.none-text {
		font-size: 28rpx;
		color: #707070;
		margin-top: 70rpx;
	}
	
}
</style>
